<?php
/**
 * Created by PhpStorm.
 * User: tanghui
 * Date: 2017/7/13
 * Time: 下午2:35
 */
require_once "../lib/FHttpex.php";

class Service_ApiBaidu{

    //const URL_SHIP="http://211.159.159.67:13888/flow/v1_0/api/expense";
    //const URL_ORDER="http://211.159.159.67:13888/flow/v1_0/order/addApiOrder";
    const URL_SHIP="http://api.kfw.net/flow/v1_0/api/expense";
    const URL_ORDER="http://api.kfw.net/flow/v1_0/order/addApiOrder";
    const URL_CANCEL = "http://api.kfw.net/flow/v1_0/order/cancel";
    const KEY="7D0gPa6dllokGQhL0DdL3Cm1XpUrWDZO";
    const ACCESS_TOKEN="90f8da20563b6fffaadabf4304b4988590c240f3";
    const CLIENT_ID="kfw_53r1rx16QeK0lyfj";
    const SHOP_ID=1455916;
    private static function buildSgin($data)
    {
        $kArr=array_keys($data);
        array_multisort($kArr);
        //print_r($data);exit;
        $tmp=[];
        if(!empty($data)){
            foreach($kArr as $val){
                $tmp[]=$val."=".$data[$val];
            }
        }
        $key=implode('&',$tmp);
        $key=$key."&key=".self::KEY;
        //print_r($key);exit;
        return strtoupper(md5($key));
    }

    public static function getShipping($data){
        $data['access_token']=self::ACCESS_TOKEN;
        $data['user_id']=self::SHOP_ID;
        $data['client_id']=self::CLIENT_ID;
        $sign=Service_ApiBaidu::buildSgin($data);
        $data['sign']=$sign;
        //print_r($data);exit;
        $res=FHttpex::post(self::URL_SHIP."?client_id=".self::CLIENT_ID,$data);
        $rstr=print_r($res,true);
        FLogger::write("getShipping|{$rstr}|","ordercb");
        /*echo self::URL_SHIP."?client_id=".self::CLIENT_ID . '<br/>';
        var_dump($data);echo '<br>';
        print_r($res);exit;*/
        return json_decode($res);
    }

    public static function addOrder($data){
        $data['access_token']=self::ACCESS_TOKEN;
        $data['user_id']=self::SHOP_ID;
        $data['client_id']=self::CLIENT_ID;
        $sign=Service_ApiBaidu::buildSgin($data);
        $data['sign']=$sign;
        //print_r($data);
        $res=FHttpex::post(self::URL_ORDER."?client_id=".self::CLIENT_ID,$data);
        $rstr=print_r($res,true);
        FLogger::write("addOrder|{$rstr}|","ordercb");
        //print_r($res);exit;
        return json_decode($res);
    }

    public static function cancelOrder($data){
        $data['access_token']=self::ACCESS_TOKEN;
        $data['user_id']=self::SHOP_ID;
        $data['client_id']=self::CLIENT_ID;
        $sign=Service_ApiBaidu::buildSgin($data);
        $data['sign']=$sign;
        //print_r($data);
        $res=FHttpex::post(self::URL_CANCEL."?client_id=".self::CLIENT_ID,$data);
        $rstr=print_r($res,true);
        FLogger::write("delOrder|{$rstr}|","ordercb");
        //print_r($res);exit;
        return json_decode($res);
    }


}